package com.dingwen.treasure.kettle.service.impl;

import cn.hutool.extra.spring.SpringUtil;
import com.dingwen.treasure.kettle.service.CategoryService;
import com.dingwen.treasure.kettle.utils.TreeUtil;
import com.dingwen.treasure.kettle.vo.CategoryVo;
import lombok.SneakyThrows;
import org.pentaho.di.repository.RepositoryDirectory;
import org.pentaho.di.repository.RepositoryDirectoryInterface;
import org.pentaho.di.repository.kdr.KettleDatabaseRepository;
import org.springframework.context.annotation.DependsOn;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * 　CategoryServiceImpl
 * 　@author dingwen
 * 　@date 2022/8/21
 */
@Order(2)
@Service
@DependsOn("kettleDatabaseRepositoryInit")
public class CategoryServiceImpl implements CategoryService {


    /**
     * kettle 数据库资源库
     */
    private KettleDatabaseRepository kettleDatabaseRepository;


    @PostConstruct
    private void initialize() {
        kettleDatabaseRepository = SpringUtil.getBean(KettleDatabaseRepository.class);
    }

    /**
     * 查询资源库目录树
     *
     * @return {@link List}<{@link CategoryVo}>
     */
    @Override
    public List<CategoryVo> getCategoryTree() {
        return TreeUtil.builder(getAllCategory(),"0");
    }

    /**
     * 得到所有目录
     *
     * @return {@link List}<{@link CategoryVo}>
     */
    @Override
    @SneakyThrows
    public List<CategoryVo> getAllCategory() {
        RepositoryDirectoryInterface directoryInterface = kettleDatabaseRepository.loadRepositoryDirectoryTree().findDirectory("/");
        List<CategoryVo> categories = new ArrayList<>(16);
        return getSubdirectories(directoryInterface,categories);
    }


    /**
     * 得到子目录
     *
     * @param repositoryDirectoryInterface 存储库目录接口
     * @return {@link List}<{@link CategoryVo}>
     */
    @SneakyThrows
    private List<CategoryVo> getSubdirectories(RepositoryDirectoryInterface repositoryDirectoryInterface,List<CategoryVo> categories) {
        int nrSubDirectories = repositoryDirectoryInterface.getNrSubdirectories();
        for (int i = 0; i < nrSubDirectories; i++) {
            // 子目录
            RepositoryDirectory subdirectory = repositoryDirectoryInterface.getSubdirectory(i);
            CategoryVo categoryVo = CategoryVo.builder()
                    .id(subdirectory.getObjectId().getId())
                    .categoryName(subdirectory.getName())
                    .parentId(subdirectory.getParent().getObjectId().getId())
                    .categoryPath(subdirectory.getPath())
                    .build();
            categories.add(categoryVo);
            RepositoryDirectoryInterface directoryInterface = kettleDatabaseRepository.loadRepositoryDirectoryTree()
                    .findDirectory(categoryVo.getCategoryPath());
            // 递归调用获取子目录
            getSubdirectories(directoryInterface,categories);
        }
        return categories;
    }
}
